package ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import service.IUserService;
import service.UserService;
import model.users;
public class RegisterDialog extends JDialog implements ActionListener{
	private JLabel label,label1,label2;
	private JTextField Account;
	private JPasswordField Password1,Password2;
	private JButton register,cancel;
	private JPanel p1;
	public RegisterDialog(){
		label=new JLabel(" 用户名 ：");
		label1=new JLabel("  密码  ：");
		label2=new JLabel("确定密码：");
		Account=new JTextField(15);
		Password1=new JPasswordField(15);
		Password2=new JPasswordField(15);
		register=new JButton("注册");
		cancel=new JButton("取消");
		
		p1 = (JPanel)this.getContentPane();
		p1=new JPanel();
		p1.add(label);p1.add(Account);//输入用户名
		p1.add(label1);p1.add(Password1);//输入密码
		p1.add(label2);p1.add(Password2);//再次确定输入密码
		p1.add(register);//注册
		p1.add(cancel);//取消
		
		label.setBounds(80,40,100,30);
		label.setFont(new Font("宋体", Font.PLAIN, 20));
		label.setForeground(Color.BLACK);
		Account.setBounds(180,40,165,30);
		
		label1.setBounds(80,75,100,30);
		label1.setFont(new Font("宋体", Font.PLAIN, 20));
		label1.setForeground(Color.BLACK);
		Password1.setBounds(180, 75, 165, 30);
		
		label2.setBounds(80,115,100,30);
		label2.setFont(new Font("宋体", Font.PLAIN, 20));
		label2.setForeground(Color.BLACK);
		Password2.setBounds(180, 115, 165, 30);
		
		register.setBounds(135,175,75,35);
		register.setFont(new Font("宋体", Font.PLAIN, 20));
		register.setForeground(Color.BLUE);
		cancel.setBounds(225, 175, 75,35);
		cancel.setFont(new Font("宋体", Font.PLAIN, 20));
		cancel.setForeground(Color.BLUE);
		
		
		this.add(p1);
		setTitle("新用户注册界面");
		setBounds(100,100,450, 300);
		getContentPane().setLayout(new BorderLayout());
		p1.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(p1, BorderLayout.CENTER);
		p1.setLayout(null);
		setVisible(true);
		setResizable(false);
		
		register.addActionListener(this);
		cancel.addActionListener(this);
	}
	public static void main(String[] args) {
		 new RegisterDialog();
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		IUserService service=null;
		users user = new users();
		String name = String.valueOf(Account.getText());
		String password = String.valueOf(Password1.getText());
		String password1 = String.valueOf(Password2.getText());
		if(e.getSource()==register){
			try{
				 service = new UserService();
			       if(Password1.equals(Password2)==true){
			    	   user.setpassword(password1);
			           user.setname(name);
			    	   if(service.insertUser(user)==true){
			    	       JOptionPane.showMessageDialog(null, "新用户注册成功！","注册成功",JOptionPane.INFORMATION_MESSAGE);
			    	       setVisible(false);
			       }   
			    	   else 
				    	   JOptionPane.showMessageDialog(null, "此用户名已存在！","注册失败",JOptionPane.ERROR_MESSAGE);
			       }
			       else {Password2.setText("");
			       JOptionPane.showMessageDialog(null, "两次输入的密码不一致！请重新输入","注册失败",JOptionPane.ERROR_MESSAGE);
			       }
			}catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		if(e.getSource()==cancel){
			setVisible(false);
		}
	}

}
